package cn.ffcs.common.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.echoice.paper.PageBean;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionSupport;

import cn.ffcs.base.web.PageControl;
import cn.ffcs.bpm.service.TransactorService;
import cn.ffcs.bpmpvc.services.AppBusinessReqService;
import cn.ffcs.bpmpvc.svc.TaskService;
import cn.ffcs.bpmpvc.web.forms.FlowTaskForm;
import cn.ffcs.bpmpvc.web.views.FlowTaskObjView;
import cn.ffcs.businessrequest.model.BusinessRequest;
import cn.ffcs.businessrequest.model.ProdAdvReq;
import cn.ffcs.businessrequest.service.IProdAdvReqService;
import cn.ffcs.product.model.ProductReference;
import cn.ffcs.product.service.IProductReferenceService;
import cn.ffcs.security.model.User;
import cn.ffcs.share.model.ShareReference;
import cn.ffcs.share.service.IShareReferenceService;
import cn.ffcs.util.context.ApplicationContextUtil;

/**
 * 业务action层<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author liuzhh
 * @version $Id$
 */
public class IndexAction extends ActionSupport
{
    /** 取数总数 **/
    private static int NUM_=5;
    /** 取数共享资料总数 **/
    private static int NUM_SHARE_=10;
    /** 产品资料服务 **/
    private IProductReferenceService productReferenceService;
    /** 共享资料服务 **/
    private IShareReferenceService shareReferenceService;
    /** 办理人服务 **/
    private TransactorService transactorService;
    /** 产品资料列表数据 **/
    private List<ProductReference> productRefs;
    /** 共享资料数据 **/
    private List<ShareReference> shareRefs;    
    /** 业务流程单数据集 **/
    private List<BusinessRequest> businessRequests;
    /** 当点用户 **/
    private User ssoUser;
    /** 改进产品需求单列表 **/
    private List<ProdAdvReq> prodAdvReqs;
    
    private List<FlowTaskObjView> flowTaskObjViewList; 
    /** 产品改进需求服务 **/
    private IProdAdvReqService prodAdvReqService;
    
    private AppBusinessReqService appBusinessReqService;
    
    public String getIndexData(){
    	 productRefs=productReferenceService.findAll(NUM_,"begin");
    	 shareRefs=shareReferenceService.findAll(NUM_SHARE_,"begin");
    	 User user=ApplicationContextUtil.getUserContextObject().getCurrUser();
    	 FlowTaskForm flowTaskForm=new FlowTaskForm();
    	 flowTaskForm.setUserId(user.getPersonID());
    	 flowTaskForm.setState(TaskService.TASK_STATE_ACTIVE);
    	 PageBean pageBean=getAppBusinessReqService().getAppBusinessReqDao().queryPageTaskList(flowTaskForm, 1, 8);
    	 flowTaskObjViewList=pageBean.getDataList();
        return SUCCESS;
    }
    
    public String getIndexDataBak(){
        int count=8;
        productRefs=productReferenceService.findAll(NUM_,"begin");
        shareRefs=shareReferenceService.findAll(NUM_SHARE_,"begin");
        User user=ApplicationContextUtil.getUserContextObject().getCurrUser();
        HashMap<String, Object> conds = new HashMap<String, Object>();
        conds.put("transactor.userId", user.getPersonID());
        businessRequests = transactorService.findByPage(conds);
        int brlength = 0;
        
        ///查询当前人待办的改进单
        Criterion cond1 = Restrictions.eq("tranStatus", "NEW");
        Criterion cond2 = Restrictions.eq("tranStatus", "REFUSE");
        Criterion cond3 = Restrictions.eq("startPerson", user.getPerson());
        Criterion cond4 = Restrictions.or(cond1, cond2);
        Criterion cond = Restrictions.and(cond4, cond3);
        prodAdvReqs=prodAdvReqService.findBy(cond);

        if(businessRequests != null && businessRequests.size() >= count)
        {
            brlength = businessRequests.size() - count;
            for(int n = 0; n < brlength; n++){
                businessRequests.remove(n);
            }
            prodAdvReqs=new ArrayList<ProdAdvReq>();
        }
        if(businessRequests != null && businessRequests.size() >= 0 && businessRequests.size()+prodAdvReqs.size() >= count)
        {
            brlength =  businessRequests.size()+prodAdvReqs.size()- count;
            for(int n = 0; n < brlength; n++){
                prodAdvReqs.remove(n);
            }
        }
        else if(prodAdvReqs!=null&&prodAdvReqs.size()+businessRequests.size() < count){
            brlength = count-businessRequests.size()-prodAdvReqs.size();
            for(int n = 0; n < brlength; n++){
                prodAdvReqs.add(new ProdAdvReq());
            }
        }
        
        return SUCCESS;
    } 
    
    public String toForum(){
        
        User currUser=ApplicationContextUtil.getUserContextObject().getCurrUser();
        ssoUser=currUser;
        
        return SUCCESS;
    }

    /**
     * 获取产品资料服务
     * @return the productReferenceService
     */
    public IProductReferenceService getProductReferenceService()
    {
        return productReferenceService;
    }

    /**
     * 设置产品资料服务
     * @param productReferenceService the productReferenceService to set
     */
    public void setProductReferenceService(
            IProductReferenceService productReferenceService)
    {
        this.productReferenceService = productReferenceService;
    }

    /**
     * @return the productRefs
     */
    public List<ProductReference> getProductRefs()
    {
        return productRefs;
    }

    /**
     * @param productRefs the productRefs to set
     */
    public void setProductRefs(List<ProductReference> productRefs)
    {
        this.productRefs = productRefs;
    }

    /**
     * @return the ssoUser
     */
    public User getSsoUser()
    {
        return ssoUser;
    }

    /**
     * @param ssoUser the ssoUser to set
     */
    public void setSsoUser(User ssoUser)
    {
        this.ssoUser = ssoUser;
    }

    public IShareReferenceService getShareReferenceService()
    {
        return shareReferenceService;
    }

    public void setShareReferenceService(
            IShareReferenceService shareReferenceService)
    {
        this.shareReferenceService = shareReferenceService;
    }

    public List<ShareReference> getShareRefs()
    {
        return shareRefs;
    }

    public void setShareRefs(List<ShareReference> shareRefs)
    {
        this.shareRefs = shareRefs;
    }

    public TransactorService getTransactorService()
    {
        return transactorService;
    }

    public void setTransactorService(TransactorService transactorService)
    {
        this.transactorService = transactorService;
    }

    public List<BusinessRequest> getBusinessRequests()
    {
        return businessRequests;
    }

    public void setBusinessRequests(List<BusinessRequest> businessRequests)
    {
        this.businessRequests = businessRequests;
    }

    public List<ProdAdvReq> getProdAdvReqs()
    {
        return prodAdvReqs;
    }

    public void setProdAdvReqs(List<ProdAdvReq> prodAdvReqs)
    {
        this.prodAdvReqs = prodAdvReqs;
    }

    public IProdAdvReqService getProdAdvReqService()
    {
        return prodAdvReqService;
    }

    public void setProdAdvReqService(IProdAdvReqService prodAdvReqService)
    {
        this.prodAdvReqService = prodAdvReqService;
    }

	public AppBusinessReqService getAppBusinessReqService() {
		return appBusinessReqService;
	}

	public void setAppBusinessReqService(AppBusinessReqService appBusinessReqService) {
		this.appBusinessReqService = appBusinessReqService;
	}

	public List<FlowTaskObjView> getFlowTaskObjViewList() {
		return flowTaskObjViewList;
	}

	public void setFlowTaskObjViewList(List<FlowTaskObjView> flowTaskObjViewList) {
		this.flowTaskObjViewList = flowTaskObjViewList;
	}

}
